package com.gitee.cui.concurrency.designmodel.masterworker;

import com.gitee.cui.util.Print;
import com.gitee.cui.util.ThreadUtil;

import java.util.concurrent.TimeUnit;

/**
 * Master-Worker模式测试类
 * @author cuiqingqiang
 * @version 1.0
 * @date 2021/12/8 16:36
 **/
public class MasterWorkerTest {

    static class SimpleTask extends Task<Integer> {
        @Override
        protected Integer doExecute() {
            Print.tcfo("task: " + getId() + " is done");
            return getId();
        }
    }

    public static void main(String[] args) {
        Master<SimpleTask, Integer> master = new Master<>(4);
        // 定期向Master中提交任务
        ThreadUtil.scheduleAtFixedRate(() -> master.submit(new SimpleTask()), 2, TimeUnit.SECONDS);
        // 定期从Master提取结果
        ThreadUtil.scheduleAtFixedRate(master::printResult, 5, TimeUnit.SECONDS);
    }
}
